package com.lbn.jfqsc.app.recruitment.item;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lbn.baseframe.base.BaseFragment;
import com.lbn.baseframe.base.BaseRecyclerAdapter;
import com.lbn.jfqsc.R;
import com.lbn.jfqsc.app.message.adapter.ActionAdapter;
import com.lbn.jfqsc.app.message.adapter.PfmAdapter;
import com.lbn.jfqsc.app.recruitment.adapter.RecruitmentAdapter;
import com.lbn.jfqsc.app.recruitment.apply.ApplyActivity;
import com.lbn.jfqsc.app.recruitment.detail.DetailActivity;
import com.lbn.jfqsc.business.bean.ActivityList;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RecruitmentItemFragment extends BaseFragment<ItemPresenter> implements ItemView,
        BaseRecyclerAdapter.OnItemClickListener, XRecyclerView.LoadingListener {

    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;
    private RecruitmentAdapter adapter;
    private int type;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_recruitment_item, null, false);
            ButterKnife.bind(this, rootView);
        }
        return rootView;
    }


    public static RecruitmentItemFragment newInstance(int type) {

        Bundle args = new Bundle();
        args.putInt("type", type);
        RecruitmentItemFragment fragment = new RecruitmentItemFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public ItemPresenter createPresenter() {
        return new ItemPresenter();
    }


    @Override
    protected void onPresenterCreated() {
        super.onPresenterCreated();

        type = getArguments().getInt("type");

        adapter = new RecruitmentAdapter(getContext(), null);
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLoadingListener(this);
        recyclerView.setRefreshing(true);
    }


    @Override
    public void onItemClick(View itemView, int pos) {
        DetailActivity.startActivity(getContext(),adapter.getItem(pos-1));
    }

    int page = 1;

    @Override
    public void onRefresh() {
        page = 1;
        mPresenter.applyList(page, type);
    }

    @Override
    public void onLoadMore() {
        page++;
        mPresenter.applyList(page, type);
    }

    @Override
    public void isMore(boolean b) {
        recyclerView.setNoMore(!b);
    }

    @Override
    public void setData(List list) {
        adapter.setData(list);
        recyclerView.refreshComplete();
    }

    @Override
    public void addData(List list) {
        adapter.setData(list);
        recyclerView.refreshComplete();
    }
}
